package com.fametal.ui.activity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.ActivityGroup;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.fametal.GlobalConstant;
import com.fametal.GlobalConstant.Settings;
import com.fametal.R;
import com.fametal.ui.utils.DialogUtils;
import com.fametal.ui.utils.Utils;

public class SignalSwitchActivity extends AbstractActivity {

	private static final String url = Settings.IP + "cgi-bin/signal_switch.cgi";

	private ActivityGroup mGroup;
	private String inputValue = "";
	private Set<String> outputSet_1;
	private Set<String> outputSet_2;
	private Set<String> outputSet_3;
	private Set<String> outputSet_4;
	private String outputValue_1 = "";
	private String outputValue_2 = "";
	private String outputValue_3 = "";
	private String outputValue_4 = "";
	private View lastInputView;
	private View lastOutputView;
	
	private Button input_video;
	private Button input_connect;
	private Button input_obligate_one;
	private Button input_obligate_two;
	private Button output_av1;
	private Button output_av2;
	private Button output_av3;
	private Button output_av4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signal_switch_main);

		mGroup = (ActivityGroup) getParent();

		outputSet_1 = new HashSet<String>();
		outputSet_2 = new HashSet<String>();
		outputSet_3 = new HashSet<String>();
		outputSet_4 = new HashSet<String>();

		TextView signal_input = (TextView) findViewById(R.id.signal_input);
		signal_input.setText(Html.fromHtml(getResources().getString(
				R.string.signal_input)));

		TextView signal_output = (TextView) findViewById(R.id.signal_output);
		signal_output.setText(Html.fromHtml(getResources().getString(
				R.string.signal_output)));
		
		input_video = (Button) findViewById(R.id.signal_video_conference);
		input_connect = (Button) findViewById(R.id.signal_connect_pc);
		input_obligate_one = (Button) findViewById(R.id.signal_obligate_one);
		input_obligate_two = (Button) findViewById(R.id.signal_obligate_two);
		output_av1 = (Button) findViewById(R.id.signal_av1);
		output_av2 = (Button) findViewById(R.id.signal_av2);
		output_av3 = (Button) findViewById(R.id.signal_av3);
		output_av4 = (Button) findViewById(R.id.signal_av4);
	}

//	public void initView() {
//		Button input_video = (Button) findViewById(R.id.signal_video_conference);
//		Button input_connect = (Button) findViewById(R.id.signal_connect_pc);
//		Button input_obligate_one = (Button) findViewById(R.id.signal_obligate_one);
//		Button input_obligate_two = (Button) findViewById(R.id.signal_obligate_two);
//		Button output_av1 = (Button) findViewById(R.id.signal_av1);
//		Button output_av2 = (Button) findViewById(R.id.signal_av2);
//		Button output_av3 = (Button) findViewById(R.id.signal_av3);
//		Button output_av4 = (Button) findViewById(R.id.signal_av4);
//
//		if (inputValue != null && !inputValue.equals("")) {
//			if (inputValue.equals("AV1")) {
//				setSectedInput(input_video, true);
//				lastInputView = input_video;
//			} else if (inputValue.equals("AV2")) {
//				setSectedInput(input_connect, true);
//				lastInputView = input_connect;
//			} else if (inputValue.equals("AV3")) {
//				setSectedInput(input_obligate_one, true);
//				lastInputView = input_obligate_one;
//			} else if (inputValue.equals("AV4")) {
//				setSectedInput(input_obligate_two, true);
//				lastInputView = input_obligate_two;
//			}
//		}
//
//		if (outputValue != null && !outputValue.equals("")) {
//			String[] outputs = outputValue.split(":");
//			for (String output : outputs) {
//				if (output.equals("AV1")) {
//					setSectedOutput(output_av1, true);
//					output_av1.setTag(true);
//				} else if (output.equals("AV2")) {
//					setSectedOutput(output_av2, true);
//					output_av2.setTag(true);
//				} else if (output.equals("AV3")) {
//					setSectedOutput(output_av3, true);
//					output_av3.setTag(true);
//				} else if (output.equals("AV4")) {
//					setSectedOutput(output_av4, true);
//					output_av4.setTag(true);
//				}
//				outputSet_1.add(output);
//			}
//		}
//
//	}
//
//	@Override
//	protected void onResume() {
//		SharedPreferences sharedPreferences = getSharedPreferences("signal",
//				Context.MODE_PRIVATE);
//		inputValue = sharedPreferences.getString("input", "");
//		outputValue = sharedPreferences.getString("output", "");
//		Log.d("david", "inputValue = " + inputValue + "; outputValue = "
//				+ outputValue);
//		initView();
//		super.onResume();
//	}
//
//	@Override
//	protected void onPause() {
//		SharedPreferences sharedPreferences = getSharedPreferences("signal",
//				Context.MODE_PRIVATE);
//		Editor editor = sharedPreferences.edit();
//		editor.putString("input", inputValue);
//		editor.putString("output", outputValue);
//		editor.commit();
//		super.onPause();
//	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.signal_video_conference:
			selectedInput(view, "AV1");
			break;
		case R.id.signal_connect_pc:
			selectedInput(view, "AV2");
			break;
		case R.id.signal_obligate_one:
			selectedInput(view, "AV3");
			break;
		case R.id.signal_obligate_two:
			selectedInput(view, "AV4");
			break;
		case R.id.signal_av1:
			if (inputIsNull())
				return;
			selectedOutput(view, "AV1");
			get();
			break;
		case R.id.signal_av2:
			if (inputIsNull())
				return;
			selectedOutput(view, "AV2");
			get();
			break;
		case R.id.signal_av3:
			if (inputIsNull())
				return;
			selectedOutput(view, "AV3");
			get();
			break;
		case R.id.signal_av4:
			if (inputIsNull())
				return;
			selectedOutput(view, "AV4");
			get();
			break;
		case R.id.settings:
			// LinearLayout container = (LinearLayout)
			// mGroup.getWindow().findViewById(R.id.signal_container);
			// mGroup.getLocalActivityManager().removeAllActivities();
			// Utils.setActivityContent(mGroup, "signal", container,
			// SystemSettingsActivity.class);
			break;
		}
	}

	public void selectedInput(View view, String value) {
		if (inputValue != null && !inputValue.equals(value)) {
			setSectedInput(view, true);
			inputValue = value;
			if (lastInputView != null)  setSectedInput(lastInputView, false);
			lastInputView = view;
			
			if(inputValue.equals("AV1")) {
				init(outputValue_1);
			}else if(inputValue.equals("AV2")) {
				init(outputValue_2);
			}else if(inputValue.equals("AV3")) {
				init(outputValue_3);
			}else if(inputValue.equals("AV4")) {
				init(outputValue_4);
			}
			
		} else {
			if((inputValue.equals("AV1") && outputSet_1.size() != 0) 
					|| (inputValue.equals("AV2") && outputSet_2.size() != 0)
					|| (inputValue.equals("AV3") && outputSet_3.size() != 0)
					|| (inputValue.equals("AV4") && outputSet_4.size() != 0)) {
				DialogUtils.error(getParent(), "错误提示", "该输入下还有输出，请先清掉输出", null);
				return;
			}
			setSectedInput(view, false);
			inputValue = "";
			lastInputView = null;
		}
	}
	
	public void init(String outputValue) {
		setSectedOutput(output_av1, false);
		output_av1.setTag(false);
		
		setSectedOutput(output_av2, false);
		output_av2.setTag(false);
		
		setSectedOutput(output_av3, false);
		output_av3.setTag(false);
		
		setSectedOutput(output_av4, false);
		output_av4.setTag(false);
		
		if (outputValue != null && !outputValue.equals("")) {
			String[] outputs = outputValue.split(":");
			for (String output : outputs) {
				if (output.equals("AV1")) {
					setSectedOutput(output_av1, true);
					output_av1.setTag(true);
				} else if (output.equals("AV2")) {
					setSectedOutput(output_av2, true);
					output_av2.setTag(true);
				} else if (output.equals("AV3")) {
					setSectedOutput(output_av3, true);
					output_av3.setTag(true);
				} else if (output.equals("AV4")) {
					setSectedOutput(output_av4, true);
					output_av4.setTag(true);
				}
			}
		}
	}

	public void selectedOutput(View view, String value) {
		Log.d("deng", "selected output");
		boolean selected = (Boolean) view.getTag() == null ? false
				: (Boolean) view.getTag();
		if (!selected) {
			setSectedOutput(view, true);
			view.setTag(true);
			if(inputValue.equals("AV1")) {
				outputSet_1.add(value);
			}else if(inputValue.equals("AV2")) {
				outputSet_2.add(value);
			}else if(inputValue.equals("AV3")) {
				outputSet_3.add(value);
			}else if(inputValue.equals("AV4")) {
				outputSet_4.add(value);
			}
		} else {
			setSectedOutput(view, false);
			view.setTag(false);
			if(inputValue.equals("AV1")) {
				outputSet_1.remove(value);
			}else if(inputValue.equals("AV2")) {
				outputSet_2.remove(value);
			}else if(inputValue.equals("AV3")) {
				outputSet_3.remove(value);
			}else if(inputValue.equals("AV4")) {
				outputSet_4.remove(value);
			}
		}
		// if (lastOutputView != null)
		// lastOutputView.setBackgroundResource(R.drawable.signal_bt_output_up);
		// lastOutputView = view;
	}

	public void setSectedInput(View view, boolean selected) {
		if (selected) {
			view.setBackgroundResource(R.drawable.signal_bt_input_down);
		} else {
			view.setBackgroundResource(R.drawable.signal_bt_input_up);
		}
	}

	public void setSectedOutput(View view, boolean selected) {
		if (selected) {
			view.setBackgroundResource(R.drawable.signal_bt_output_down);
		} else {
			view.setBackgroundResource(R.drawable.signal_bt_output_up);
		}
	}

	public void get() {
		String outputValue = "";
		if(inputValue.equals("AV1")) {
			for (String value : outputSet_1) {
				outputValue_1 = outputValue_1 + value + ":";
			}
			outputValue = outputValue_1;
		}else if(inputValue.equals("AV2")) {
			for (String value : outputSet_2) {
				outputValue_2 = outputValue_2 + value + ":";
			}
			outputValue = outputValue_2;
		}else if(inputValue.equals("AV3")) {
			for (String value : outputSet_3) {
				outputValue_3 = outputValue_3 + value + ":";
			}
			outputValue = outputValue_3;
		}else if(inputValue.equals("AV4")) {
			for (String value : outputSet_4) {
				outputValue_4 = outputValue_4 + value + ":";
			}
			outputValue = outputValue_4;
		}
		String target = url + "?input=" + inputValue + "&output=" + outputValue;
		Utils.get(getParent(), target, false);
	}

	public boolean inputIsNull() {
		if (inputValue == null || inputValue.equals("")) {
			Log.d("deng", "inputValue = " + inputValue);
			DialogUtils.error(getParent(), "错误提示", "请选择输入", null);
			return true;
		}
		return false;
	}
}
